// 记录调用 request 方法的次数
let ajaxTimes = 0
import {
	changeLastPage
} from './utils.js'
// 请求地址的基本路径
//const baseUrl = 'https://lk.cnmr.vip/api/'
const baseUrl = 'http://127.0.0.1/api/'
//const baseUrl = 'http://wx.javazhou.icu/api/'
//const ossUrl="https://hnjzsoft.oss-cn-beijing.aliyuncs.com/lengku/";
const ossUrl = "http://oss.91xiaokong.com/fireControl/";
var showModalFlag = false;

function request(params, contentType) {
	// 显示加载数据的提示
	uni.showLoading({
		title: '加载中',
		mask: true
	})
	let content_type = "";
	if (contentType == 'query') {
		content_type = "application/x-www-form-urlencoded"
	} else if (contentType == 'json') {
		content_type = "application/json"
	}
	console.log(baseUrl + params.url + "_请求参数", params)

	return new Promise(function(resolve, reject) {
		uni.request({
			method: params.methods,
			url: baseUrl + params.url,
			data: params.data,
			header: {
				'content-type': content_type,
				'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			success(res) {
				uni.hideLoading();
				console.log(baseUrl + params.url + '_响应：', res.data);
				if (res.data.code == 401) {
					uni.showToast({
					    title: '登录已过期,请重新登录',
					    duration: 2000,
						icon:'none'
					});
				}
				if (res.data.code == 401 && !showModalFlag) {
					changeLastPage();
					showModalFlag = true;
					uni.showModal({
						showCancel: false,
						content: "登录已过期,请重新登录",
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/index',
								});
							}
						}
					})
					return
				}
				resolve(res)
			},
			fail(err) {
				reject(err)
			},
			complete() {
				// 停止加载数据的提示
			}
		})
	})
}

/**
 * 根据字典类型获取字典列表
 */
function getDictDataByDictType(dictType) {
	return request({
		methods: "POST",
		url: "dict/data/selectDictDataList",
		data: {
			dictType: dictType
		}
	}, "query")
}
/**
 * 根据字典值查询字典名
 * @param {Object} dictType
 * @param {Object} dictValue
 */
function selectDictLabel(dictType, dictValue) {
	return request({
		methods: "POST",
		url: "dict/data/selectDictLabel",
		data: {
			dictType: dictType,
			dictValue: dictValue
		}
	}, "query")
}


export {
	request,
	ossUrl,
	getDictDataByDictType,
	selectDictLabel
}
